package com.yc.navy.snacknet.controller;

import java.io.IOException;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.yc.navy.snacknet.util.ConstantIfo;
import com.yc.navy.snacknet.util.RequestParamUtul;
import com.yc.navy.snacknet.util.SendEmailUtil;
import com.yc.navy.snacknet.vo.ResultVO;
import com.yc.navy.snacknet.bean.MemberInfo;
import com.yc.navy.snacknet.enums.ResultEnum;
import com.yc.navy.snacknet.service.IMemberInfoService;


@WebServlet("/menber/*")
public class MemberInfoController extends BaseController{
	
	private static final long serialVersionUID = 5531818357841494711L;
	
	//发送验证码
	public void sendCode(HttpServletRequest request, HttpServletResponse response) throws  IOException {
		String code = "";
		Random rd = new Random();
		while(code.length() < 6) {
			code += rd.nextInt(9);
		}
		
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		SendEmailUtil sendEmailUtil = (SendEmailUtil) context.getBean("sendEmailUtil");
		String receive = request.getParameter("email");
		String nickName = request.getParameter("nickName");
		HttpSession session = request.getSession();
		if(sendEmailUtil.send(receive, code, nickName)) {
			session.setAttribute(ConstantIfo.CURRENTEMALLCODE, code);
			System.out.println(session.getAttribute(ConstantIfo.CURRENTEMALLCODE) +   code);
			TimerTask task = new TimerTask() {
				@Override
				public void run() {
					session.removeAttribute(ConstantIfo.CURRENTEMALLCODE);
				}
			};
			System.out.println(task);
			Timer timer = new Timer();
			timer.schedule(task, 180000);
			this.send(response, new ResultVO(ResultEnum.SUCCESS));
			return;
		}	
		this.send(response, new ResultVO(ResultEnum.ERROP));
	}
	
	//注册
	public void reg(HttpServletRequest request, HttpServletResponse response) throws  IOException {
		Object obj = request.getSession().getAttribute(ConstantIfo.CURRENTEMALLCODE);
		if(obj == null) {
			this.send(response, new ResultVO(ResultEnum.CODE_TIMEOUT));
			return;
		}
		
		MemberInfo mf = RequestParamUtul.getParams(MemberInfo.class, request);
		String code = (String) obj;
		if(!code.equals(mf.getRealName())) {
			this.send(response, new ResultVO(ResultEnum.CHECK_ERROR));
			return;
		}
		
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		IMemberInfoService userInfoService = (IMemberInfoService) context.getBean("memberinfoServiceImpl");
		int result = userInfoService.reg(mf);
		System.out.println(result);
		if(result == -1) {
			this.send(response, new ResultVO(ResultEnum.CHECK_ERROR));
			return;
		}
		
		if(result > 0) {
			this.send(response, new ResultVO(ResultEnum.SUCCESS));
			return;
		}
		this.send(response, new ResultVO(ResultEnum.ERROP));
	}
	
	//登录
	public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		MemberInfo mf = RequestParamUtul.getParams(MemberInfo.class, request);
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		IMemberInfoService memberInfoService = (IMemberInfoService) context.getBean("memberinfoServiceImpl");
		MemberInfo member = memberInfoService.login(mf);
		if(member != null) {
			request.getSession().setAttribute("currentLoginUser", member);
			this.send(response, 200, "成功");
			return;
		}
		this.send(response, 500, "失败");
	}
	
	public void checkLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object obj = request.getSession().getAttribute("currentLoginUser");
		if(obj == null) {
			this.send(response, 600, "未登录");
			return;
		}
		this.send(response, 200, "成功");
	}
}
